package com.cp.core.controller.admin;

import com.cp.common.exception.Assert;
import com.cp.common.result.ResponseEnum;
import com.cp.core.pojo.entity.IntegralGrade;
import com.cp.core.service.IntegralGradeService;
import com.cp.common.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ming
 * @create 2022/7/27
 * @description: 积分等级表
 */
@Api(tags = "积分等级管理")
@RestController
@RequestMapping("/admin/core/integralGrade")
//@CrossOrigin
public class AdminIntegralGradeController {
    @Resource
    private IntegralGradeService integralGradeService;
    @ApiOperation("积分等级列表")
    @GetMapping("/list")
    public R listAll(){
        List<IntegralGrade> list = integralGradeService.list();
        return R.ok().data("list",list).message("获取列表成功");
    }
    @ApiOperation(value = "根据id删除等级记录",notes = "逻辑删除记录")
    @DeleteMapping("/remove/{id}")
    public R removeById(@ApiParam(value = "数据id",example = "100",required = true) @PathVariable("id") String id){
        boolean b = integralGradeService.removeById(id);
        if(b){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }
    @ApiOperation(value = "根据id新增",notes = "新增等级")
    @PostMapping("/save")
    public R save(@RequestBody
                  @ApiParam(value = "等级列表对象",required = true) IntegralGrade integralGrade){
        Assert.notNull(integralGrade.getBorrowAmount(),ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        boolean result = integralGradeService.save(integralGrade);
        if(result){
            return R.ok().message("保存等级成功");
        }else{
            return R.ok().message("保存等级失败");
        }
    }
    @ApiOperation("根据id查询等级信息")
    @GetMapping("/get/{id}")
    public R getById(@ApiParam(value = "数据id",required = true) @PathVariable("id") Integer id){
        IntegralGrade integralGrade = integralGradeService.getById(id);
        if(integralGrade!=null){
            return R.ok().message("数据获取成功").data("record",integralGrade);
        }else{
            return R.error().message("查询失败");
        }
    }
    @ApiOperation(value = "根据id更新",notes = "更新等级")
    @PostMapping("/update")
    public R update(@RequestBody
                  @ApiParam(value = "等级列表对象",required = true) IntegralGrade integralGrade){
        boolean result = integralGradeService.updateById(integralGrade);
        if(result){
            return R.ok().message("更新等级成功");
        }else{
            return R.ok().message("更新等级失败");
        }
    }
}
